// pages/home/home.js
import request from '../../utils/request'
Page({
  // 获取轮播图数据
  async getBannerList() {
    const res = await request({
      url: '/banner',
      data: {
        type: 2
      }
    })
    if (res.code === 200) {
      this.setData({
        bannerList: res.banners
      })
      // console.log(this.data.bannerList)
    }
  },
  // 获取推荐歌曲数据
  async getSongsList() {
    const songList = await request({
      url: '/personalized',
      data: {
        limit: 20
      }
    })
    if (songList.code === 200) {
      this.setData({
        songsList: songList.result
      })
      // console.log(this.data.songsList)
    }
  },
  // 获取排行榜歌曲数据
  async getMusicRanking() {
    const gatherMusic = []
    const gatherName = []
    for (let i = 0; i < 5;i++) {
      const res = await request({
        url: '/top/list',
        data: {
          idx: i
        }
      })
      // console.log(res)
      if (res.code === 200) {
        const newRes = res.playlist.tracks.slice(0, 3)
        const name = res.playlist.name
        // console.log(newRes,'res')
        gatherMusic.push(newRes)
        gatherName.push(res.playlist.name)
        this.setData({
          musicRanking:gatherMusic,
          rankingName:gatherName
        })
      }
    }
    // console.log(gatherMusic)
  },
  //点击每日推荐
  RecommendedDaily(){
    wx.navigateTo({
      url: '/pages/recommendSong/recommendSong',
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    songsList: [],
    musicRanking:[],
    rankingName:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBannerList()
    this.getSongsList()
    this.getMusicRanking()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})